﻿using ProTrack.Entity;
using ProTrack.IRepository;
using ProTrack.IServices.Examine;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace ProTrack.Services.Examine
{
    public class QcWayServices : IQcWayServices
    {
        public QcWayServices (IBaseRepository db)
        {
            Db = db;
        }
        public IBaseRepository Db { get; }
        public async Task<bool> AddAsync(QcWay model)
        {
            var result = await Db.InsertAsync(model);
            return result;
        }

        public async Task<bool> DeleteAsync(int[] Ids)
        {
            string sql = "delete QcWay where Id in @Ids";
            var result = await Db.DeleteAsync(sql, new { Ids });
            return result;
        }

        public async Task<bool> DeleteAsync(int Id)
        {
            string sql = "delete QcWay where Id = @Id";
            var result = await Db.DeleteAsync(sql, new { Id });
            return result;
        }

        public async Task<IEnumerable<QcWay>> GetAllAsync()
        {
            string sql = "select * from QcWay";
            var list = await Db.QueryAllAsync<QcWay>(sql);
            return list;
        }
        public async Task<IEnumerable<QcWay>> GetAsync(int PageIndex, int PageSize, string InspectCode, string InspectName)
        {
                string sql = "select * from QcWay where 1=1 ";
                if (!string.IsNullOrEmpty(InspectCode))
                {
                    sql += " and InspectCode like @InspectCode ";
                }

                if (!string.IsNullOrEmpty(InspectName))
                {
                    sql += " and InspectName like @InspectName ";
                }
                sql += " order by CreateTime desc offset @PageSize*(@PageIndex-1) row fetch next @PageSize rows only ";

                var list = await Db.QueryAsync<QcWay>(sql,new {PageIndex, PageSize, InspectCode=$"%{InspectCode}%", InspectName=$"%{InspectName}%" });
                return list;
            }

        public async Task<int> GetCountAsync(string InspectCode, string InspectName)
        {
            string sql = "select count(*) from QcWay where 1=1";
            if (!string.IsNullOrEmpty(InspectCode))
            {
                sql += " and InspectCode like @InspectCode ";
            }

            if (!string.IsNullOrEmpty(InspectName))
            {
                sql += " and InspectName like @InspectName ";
            }
            var count = await Db.GetRecoreCountAsync(sql, new { InspectCode = $"%{InspectCode}%", InspectName = $"%{InspectName}%" });
            return count;
        }

        public async Task<bool> UpdateAsync(QcWay model)
        {
            var result = await Db.UpdateAsync(model);
            return result;
        }
    }
}
